import { toast, confirm, dateFormat, wxfileTobase64 } from '../../../utils/util'
import { getMaterialLong, addMaterialLong, removeMaterialLong, getMaterialDay, addMaterialDay, removeMaterialDay } from '../../api/insurance';
Page({
    data: {
        list: {},
        insureCompany: '',
        reportCaseId: '',
        insureType: '',
        axios: {
            long: {
                getMaterial: getMaterialLong,
                addMaterial: addMaterialLong,
                removeMaterial: removeMaterialLong
            },
            day: {
                getMaterial: getMaterialDay,
                addMaterial: addMaterialDay,
                removeMaterial: removeMaterialDay
            }
        },
        compressImgs: []
    },
    onLoad(params) {
        this.setData({
            insureCompany: params.insureCompany,
            reportCaseId: params.id,
            insureType: params.insureType
        })
        this.getData()
    },
    ready() {

    },
    doUpload(url, type) {
        wxfileTobase64(url).then(base => {
            const query = {
                categoryType: type,
                reportCaseId: this.data.reportCaseId,
                file: base
            }
            this.data.axios[this.data.insureType].addMaterial(query).then(res => {
                this.getData()
            })
        })
    },
    // 上传视频
    upload(event) {
        const item = event.currentTarget.dataset.item
        wx.chooseImage({
            count: 1,
            sourceType: ['album', 'camera'],
            sizeType: ['compressed'],
            success: (res) => {
                if (item.files.length >= item.limitSize) {
                    toast(`最多上传${item.limitSize}张图片`)
                    return
                }
                const fileType = res.tempFilePaths[0].split('.')[1].toLocaleLowerCase()
                const acceptList = ['png', 'jpg', 'jpeg']
                if (!acceptList.find(i => i === fileType)) {
                    toast('请上传格式为png，jpg，jpeg的图片文件')
                    return
                }
                const size = res.tempFiles[0].size
                if (size < 10 * 1024 * 1024) {
                    this.doUpload(res.tempFilePaths[0], item.type)
                } else {
                    wx.compressImage({
                        src: res.tempFilePaths[0], // 图片路径
                        quality: 70, // 压缩质量
                        success: (res) => {
                            this.doUpload(res.tempFilePath, item.type)
                        }
                    })
                }
            }
        })
    },
    // 视频，图片删除
    onDelete(event) {

        confirm('确认删除该图片么？').then(res => {
            let { fileid } = event.currentTarget.dataset
            console.log(2121, event.currentTarget.dataset);
            this.data.axios[this.data.insureType].removeMaterial(fileid).then(res => {
                toast('删除成功')
                this.getData()
            })
            // item.files.splice(index, 1)
            // this.setData({
            //     ['list.' + item.prop + '.value']: item.value
            // })
        })


    },
    // 点击图片放大
    onPreview(event) {
        const { url } = event.currentTarget.dataset
        wx.previewImage({
            urls: [url]
        })
    },
    onPlay(event) {
        // 获取 video 上下文 videoContext 对象
        this.videoContext = wx.createVideoContext(event.currentTarget.id, this)
        // 进入全屏状态
        this.videoContext.requestFullScreen()
        return 0
    },
    onSubmit() {
        for (const key in this.data.list) {
            // if (this.data.list[key].required && !this.data.list[key].value.length) {
            //     toast(`${this.data.list[key].name}材料不能为空`)
            //     return
            // }

            this.data.list[key].value.forEach((i, index) => {
                i.fileName = this.data.list[key].name + (index + 1)
            })
        }
        console.log(111, this.data.list);
    },
    getData() {
        this.data.axios[this.data.insureType].getMaterial({
            insureCompany: this.data.insureCompany,
            reportCaseId: this.data.reportCaseId
        }).then(res => {
            let data = {}
            res.forEach(i => {
                data[i.type] = {
                    ...i,
                    required: !!i.isRequired
                }
            });
            this.setData({
                list: data
            })
        })
    },

    // handleBeforeLeave(res) {
    //     confirm('您还有必填的资料未上传，是否确认退出？').then(res => {
    //         console.log(2333);
    //         wx.navigateBack()
    //     })
    // }
})
